package cn.fl.customer.utils;

import java.util.HashMap;
import java.util.Map;
import java.nio.charset.Charset;

/**
 * 生成xml报文的工具类
 *
 * @Author: huangkailun
 * @Date: 2020-06-09 14:56
 */
public class XmlUtils {

    /**
     * 产生模型对应的XML数据
     *
     * @param charset 编码
     * @return
     */
    public static String createXml(Charset charset, String businessId, String comment, Map<String, String> xmlParam) {
        StringBuilder builder = new StringBuilder();
        builder.append("<?xml version=\"1.0\" encoding=\"").append(charset.displayName()).append("\"?>");
        builder.append("<business id=" + "\"" + businessId + "\"" + " comment=" + "\"" + comment + "\"" + ">");
        builder.append("<body yylxdm=\"1\">");
        if (xmlParam != null) {
            for (Map.Entry<String, String> keyVal : xmlParam.entrySet()) {
                if (keyVal != null) {
                    builder.append("<").append(keyVal.getKey()).append(">");
                    builder.append(keyVal.getValue() != null ? keyVal.getValue() : "");
                    builder.append("</").append(keyVal.getKey()).append(">");
                }
            }
        }
        builder.append("</body>");
        builder.append("</business>");
        return builder.toString();
    }

    public static void main(String[] args) {
        Map<String, String> xmlParam = new HashMap<>();
        xmlParam.put("fplxdm", "");
        xmlParam.put("top", "打印上边距");
        xmlParam.put("left", "打印左边距");
        System.out.println(createXml(Charset.forName("GBK"), "20002", "税控钥匙信息查询", xmlParam));
    }

}
